package com.krain.ddbb.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.krain.corelibrary.helper.imageloader.ImageLoader;
import com.krain.ddbb.R;
import com.krain.corelibrary.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 可多选的相册
 * 
 * @author DaleLiu
 * 
 */
public class MultipleAlbumAdpter extends BaseAdapter {
    private ArrayList<String> imageList = new ArrayList<String>();
    private boolean           isChoice[];
    private Context           context;
    private ImageLoader       load;

    private int               height;
    private int               width;

    public MultipleAlbumAdpter(List<String> imageList, Context context, ImageLoader load) {
        if (imageList != null) {
            this.imageList.addAll(imageList);
        }
        this.load = load;
        int size = imageList.size();
        isChoice = new boolean[size];
        for (int i = 0; i < size; i++) {
            isChoice[i] = false;
        }
        this.context = context;
        width = (UIUtil.getWindowWidth(context) - UIUtil.dip2px(context, 20)) / 3;
        height = width;
    }

    // 获取选中图片
    public ArrayList<String> getChoosePhtoto() {
        ArrayList<String> chooseData = new ArrayList<String>();
        for (int i = 0; i < isChoice.length; i++) {
            if (isChoice[i]) {
                chooseData.add(getItem(i));
            }
        }
        return chooseData;
    }
    public boolean isChoosed(int pos){
        return isChoice[pos];
    }
    // 获取选中图片的个数
    public int getChooseNum() {
        int count = 0;
        for (int i = 0; i < isChoice.length; i++) {
            if (isChoice[i]) {
                count++;
            }
        }
        return count;
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public String getItem(int arg0) {
        return imageList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View view, ViewGroup arg2) {
        GetView getView = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_mul_album, null);
            getView = new GetView();
            getView.mImage = (ImageView) view.findViewById(R.id.image);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
            getView.mImage.setLayoutParams(layoutParams);
            getView.mTvChoose = (TextView) view.findViewById(R.id.choose_tv);
            getView.mTvChoose.setBackgroundResource(R.mipmap.ic_footprintchecked);
            //IconfontUtil.setIcon(context, getView.mTvChoose, "#fa5728", 30, AnanzuIcon.OK);
            view.setTag(getView);
        } else {
            getView = (GetView) view.getTag();
        }
        if (isChoice[position]) {
            getView.mTvChoose.setVisibility(View.VISIBLE);
        } else {
            getView.mTvChoose.setVisibility(View.GONE);
        }
        load.loadImage(context, getView.mImage, getItem(position), R.mipmap.add_id_card, width, height, false);
        return view;
    }

    class GetView {
        ImageView mImage;
        TextView  mTvChoose;
    }

    public void choiceState(int post) {
        isChoice[post] = isChoice[post] == true ? false : true;
        this.notifyDataSetChanged();
    }
}
